package cn.jn.lhm.uc.handler;

import cn.jn.lhm.uc.entity.UcenterMember;
import cn.jn.lhm.uc.entity.vo.RegisterVo;
import cn.jn.lhm.uc.service.UcService;
import cn.jn.lhm.utils.JwtUtils;
import cn.jn.lhm.utils.R;
import cn.jn.lhm.utils.order.UcOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/10 - 21:25
 */
@RestController
@RequestMapping("/uc")
@Slf4j
public class UcHandler {

    @Resource
    private UcService ucService;

    @GetMapping("/getInfo")
    public R getUserInfoByToken(HttpServletRequest request){
        //根据token获取对应的userId
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        log.info("获取到的userId值为：" + userId);
        return R.ok().data("data", ucService.getUserInfo(userId));
    }

    /**
     * 用于RPC远程调用的接口
     * @param userId 用户ID
     * @return 课程的order
     */
    @GetMapping("/get/order/info/{userId}")
    public UcOrder getUserInfoByUserId(@PathVariable("userId")String userId){
        UcenterMember member = ucService.getUserInfo(userId);
        UcOrder order = new UcOrder();
        BeanUtils.copyProperties(member, order);
        return order;
    }

    /**
     * RPC远程调用接口，用来获取当天的注册人数
     * @param day 日期
     * @return
     */
    @GetMapping("/get/statistics/info/{day}")
    public R countRegisterNumber(@PathVariable("day")String day){
        Integer count = ucService.countRegister(day);
        return R.ok().data("data", count);
    }

    @PostMapping("/login")
    public R UserLogin(@RequestBody UcenterMember member){
        String token = ucService.loginUser(member);
        return R.ok().data("data",token);
    }

    @PostMapping("/register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        ucService.registerUser(registerVo);
        return R.ok();
    }

}
